package com.ruoyi.web.controller.zhuangxiucontroller.shopController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductPicInfo;
import com.ruoyi.system.service.zhaungxiuservice.shopService.ProductPicInfoService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/shopping/productPict")
public class ProductPicInfoController extends BaseController {

    @Autowired(required = false)
    private ProductPicInfoService productPicInfoService;

    /**
     * 查询商品图片信息列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:master:listppi')")
    @GetMapping("/listppi/{productId}")
    public List<ProductPicInfo> listppi(@PathVariable("productId") Integer productId)
    {
        return productPicInfoService.selectProductPicInfoList(productId);
    }

    @PreAuthorize("@ss.hasPermi('shopping:master:picquery')")
    @GetMapping(value = "/{productPicId}")
    public AjaxResult getInfo(@PathVariable("productPicId") Integer productPicId)
    {
        return AjaxResult.success(productPicInfoService.selectProductPicInfoById(productPicId));
    }

    /**
     * 新增商品图片信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:master:picadd')")
    @Log(title = "商品详情图片信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductPicInfo productPicInfo)
    {
        Integer numproductIdPic = productPicInfo.getNumproductIdPic();
        productPicInfo.setProductId(numproductIdPic);
        productPicInfo.setModifiedTime(new Date());
        productPicInfo.setPicStatus(1);
        return toAjax(productPicInfoService.insertProductPicInfo(productPicInfo));
    }

    /**
     * 修改商品图片信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:master:picedit')")
    @Log(title = "商品详情图片信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductPicInfo productPicInfo)
    {
        return toAjax(productPicInfoService.updateProductPicInfo(productPicInfo));
    }





//    @PreAuthorize("@ss.hasPermi('order:master:uoloadppi')")
//    @PostMapping("/multiPicUpload")
//    public String multPicUpload(MultipartFile file, HttpServletRequest request) throws Exception {
//        String folder ="D://noticeFile/img/";
//        File imagFolder = new File(folder);
//
////        获取随机字符串 GetRandomString
////        file.getOriginalFilename()得到上传时bai的文件名。
////        substring 截取字符串
//        File f = new File(imagFolder, getRandomString(6) + file.getOriginalFilename()
//                .substring(file.getOriginalFilename().length() - 4));
//        if (!f.getParentFile().exists()){
////            mkdirs()可以建立多级文件夹
//            f.getParentFile().mkdirs();
//            try {
////              将file移动到targetfile目录下
//                file.transferTo(f);
//                System.err.println(file.getOriginalFilename());
//
//                String imgURL ="http://localhost:8080/api/file/" +f.getName();
//                //直接保存数据库
//                ProductPicInfo productPicInfo = new ProductPicInfo();
//                productPicInfo.setProductId(1);
//                productPicInfo.setPicUrl(imgURL);
//                productPicInfo.setPicDesc(file.getOriginalFilename());
//                productPicInfoService.insertProductPicInfo(productPicInfo);
//            }catch (IOException e){
//                e.printStackTrace();
//                return "";
//            }
//        }
//        return null;
//    }
//    //获得指定长度的随机字符串
//    public String getRandomString(int length){
//        String base ="abcdefghijklmnopqrstuvwxyz0123456789";
//        Random random = new Random();
//        StringBuffer sb = new StringBuffer();
//        for (int i =0; i< length; i++){
//            int number = random.nextInt(base.length());
//            sb.append(base.charAt(number));
//        }
//
//        return sb.toString();
//    }

    /**
     * 删除商品图片信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:master:removeppi')")
    @Log(title = "商品图片信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productPicIds}")
    public AjaxResult remove(@PathVariable Integer[] productPicIds) throws Exception
    {
        try {
            productPicInfoService.deleteProductPicInfoByIds(productPicIds);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 上传图片
     * @param file
     * 
     * @return
     */
    @PostMapping("/uploadImage")
    public Map<String,Object> up(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai/";
        return FileUtil.uploadImage(file,url);
    }




}
